using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace Triumph
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            Layout_Resize();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Layout_Resize();
            OnLoad();
        }

        private void comboBoxCurrentDirectory_Leave(object sender, EventArgs e)
        {
            // string _txt = comboBoxCurrentDirectory.Text;
            if (comboBoxCurrentDirectory.Text != fileSystemWatcherCurrent.Path)
                comboBoxCurrentDirectory.Text = fileSystemWatcherCurrent.Path;
        }

        private void comboBoxCurrentDirectory_TextChanged(object sender, EventArgs e)
        {
            UpdateWatcher();
        }

        private void splitContainerMain_SplitterMoved(object sender, SplitterEventArgs e)
        {
            tabControl1.Refresh();
        }

        private void debugToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void textBoxFileRenameFormat_TextChanged(object sender, EventArgs e)
        {
            if (checkBoxContinousRename.Checked)
                FormatUpdate();
        }

        private void textBoxFileRenameFormat_Leave(object sender, EventArgs e)
        {
            if (!checkBoxContinousRename.Checked)
                FormatUpdate();
        }

        private void textBoxFileRenameFormat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                FormatUpdate();
        }

        private void comboBoxRenameFix_TextChanged(object sender, EventArgs e)
        {
            FormatUpdate();
        }

        private void comboBoxRenameCasing_TextChanged(object sender, EventArgs e)
        {
            FormatUpdate();
        }

        private void buttonUpdateRenames_Click(object sender, EventArgs e)
        {
            FormatUpdate();
        }

        private void buttonRenameFiles_Click(object sender, EventArgs e)
        {
            fileSystemWatcherCurrent.EnableRaisingEvents = false;

            foreach (ListViewItem lvi in listViewOutput.Items)
            {
                if (lvi.Checked)
                {
                    FileInfo _file = new FileInfo(lvi.SubItems[2].Text);

                    string _newShortName = lvi.SubItems[0].Text;
                    string _newname = Path.Combine(_file.Directory.FullName, _newShortName);
                    if (!File.Exists(_newname))
                        _file.MoveTo(_newname);
                    else
                    {
                        string _nonDupe = _newname;
                        int _cnt = 1;
                        while (File.Exists(_nonDupe))
                        {
                            _nonDupe = StripExtensionFromFileName(_newname, _file.Extension);
                            _nonDupe += " (" + _cnt.ToString() + ")" + _file.Extension;
                        }

                        if (!File.Exists(_nonDupe))
                            _file.MoveTo(_nonDupe);
                    }
                        /* File existed so we'll have to add (#) to the end */
                }
            }

            UpdateFiles();
            UpdateOutput();
        }

        private void listViewOutput_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            if (e.Label == null)
                return;

            /* Check for illegal chars */
            string text = "Edit cancelled, invalid chars in filename\r\n\r\nCan not contain: ";
            bool cancel = false;
            foreach (char chr in Path.GetInvalidFileNameChars())
            {
                text = text + chr.ToString() + " "; // This inserts null and other crap so can't really use it untill shit chars are removed
                if (e.Label.Contains(chr.ToString()))
                {
                    cancel = true;
                }
            }

            if (cancel)
            {
                MessageBox.Show("Edit cancelled, invalid chars in filename", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.CancelEdit = true;
                return;
            }

            listViewOutput.Items[e.Item].Checked = e.Label != listViewOutput.Items[e.Item].SubItems[1].Text;
        }
    }
}